package org.lakeside.outreach.server;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.lakeside.outreach.client.NotLoggedInException;
import org.lakeside.outreach.client.Project;
import org.lakeside.outreach.client.ProjectService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ProjectServiceImpl extends RemoteServiceServlet implements ProjectService {

	private static final Logger LOG = Logger.getLogger(ProjectServiceImpl.class.getName());
	private static final PersistenceManagerFactory PMF = 
		JDOHelper.getPersistenceManagerFactory("transactions-optional");
	
	public void addProject(String name, String description) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		try {
			pm.makePersistent(new Project(name, description, getUser()));
		} finally {
			pm.close();
		}
	}
	
	public void removeProject(Project deleted) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		try {
			long deleteCount = 0;
			Query q = pm.newQuery(Project.class);
			//q.declareParameters("com.google.appengine.api.users.User u");
			List<Project> projects = (List<Project>) q.execute();
			System.out.println(projects.toString());
			for(Project project: projects) {
				System.out.println(deleted.getName() + " == " + project.getName());
				if(deleted.getName().equals(project.getName())) {
					deleteCount++;
					pm.deletePersistent(project);
				}
			}
			if(deleteCount != 1) {
				LOG.log(Level.WARNING, "removedProject deleted "+deleteCount+" Projects");
			}
		} finally {
			pm.close();
		}
	}
	
	public List<Project> getProjects() throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		List<Project> projects = null;
		System.out.println("Querying...");
		try {
			Query q = pm.newQuery(Project.class);
			//q.declareParameters();
			q.setOrdering("createDate");
			List<Project> result = (List<Project>) q.execute();
			String ouput = result.toString();
			projects = new ArrayList<Project>();
			for(Project project: result) {
				String name = project.getName();
				projects.add(project);
			}
		} finally {
			pm.close();
		}
		return projects;
	}
	
	private void checkLoggedIn() throws NotLoggedInException {
		if(getUser() == null) {
			throw new NotLoggedInException("Not Logged In.");
		}
	}
	
	private User getUser() {
		UserService userService = UserServiceFactory.getUserService();
		return userService.getCurrentUser();
	}
	
	private PersistenceManager getPersistenceManager() {
		return PMF.getPersistenceManager();
	}
}
